/*
* $Id: TableHeaderRow.cpp 13 2010-01-10 00:27:44Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "TableHeaderRow.h"
#include "TableHeader.h"

CTableHeaderRow::CTableHeaderRow()
: m_posX(0), m_cy(0), m_sizeCx(0){}
CTableHeaderRow::~CTableHeaderRow(){}

void CTableHeaderRow::Clear(){
	for(std::vector<CTableHeader*>::iterator it = m_aHeaders.begin(); it != m_aHeaders.end(); it++)
		delete *it;
	m_aHeaders.clear();
}

tableHeader* CTableHeaderRow::AddHeader(const std::tstring& strName, int nId){
	CTableHeader* header = new CTableHeader(strName, nId);
	if(!m_aHeaders.empty()){
		header->m_pBack = m_aHeaders.back();
		((tableHeader*)(header->m_pBack))->m_pNext = header;
	}
	m_aHeaders.push_back(header);
	if(m_cy < header->GetSize().cy)
		m_cy = header->GetSize().cy;
	return header;
}

void CTableHeaderRow::Paint(CDC* dc, CRect rect){
	for(std::vector<CTableHeader*>::iterator it = m_aHeaders.begin(); it != m_aHeaders.end(); it++){
		if((*it)->GetPos().x > rect.right)
			break;
		if((*it)->GetPos().x + (*it)->GetSize().cx < rect.left)
			continue;
		(*it)->Paint(dc);
	}
}

void CTableHeaderRow::SetPosX(int x){
	m_posX = x;
	if(m_aHeaders.size())
		(*m_aHeaders.begin())->SetPos(CSize(m_posX, (*m_aHeaders.begin())->GetPos().y), true);
}

void CTableHeaderRow::NormalizeHeader(){
	m_cy = 0;
	m_sizeCx = 0;
	for(std::vector<CTableHeader*>::iterator it = m_aHeaders.begin(); it != m_aHeaders.end(); it++){
		m_sizeCx += (*it)->GetSize().cx;
		int cy = (*it)->GetDefaultSize((*it)->GetSize().cx).cy;
		if(m_cy < cy)
			m_cy = cy;
	}
	for(std::vector<CTableHeader*>::iterator it = m_aHeaders.begin(); it != m_aHeaders.end(); it++)
		(*it)->SetSize(CSize((*it)->GetSize().cx, m_cy));
	if(m_aHeaders.size())
		(*m_aHeaders.begin())->SetPos(CSize(m_posX, (*m_aHeaders.begin())->GetPos().y), true);
}

bool CTableHeaderRow::IsSetCursor(CPoint point, CTableHeader** pTableHeader){
	for(std::vector<CTableHeader*>::iterator it = m_aHeaders.begin(); it != m_aHeaders.end(); it++){
		if((*it)->IsAtBorder(point)){
			if(pTableHeader)
				*pTableHeader = *it;
			return true;
		}
	}
	if(pTableHeader)
		*pTableHeader = NULL;
	return false;
}

int CTableHeaderRow::GetWidthTable(){
	return m_sizeCx;
}

int CTableHeaderRow::GetHeightHeader(){
	return m_cy;
}